package com.fyk.service.Impl;

import com.fyk.entity.Book;
import com.fyk.entity.User;
import com.fyk.entity.UserRole;
import com.fyk.mapper.BookMapper;
import com.fyk.mapper.UserMapper;
import com.fyk.mapper.UserRoleMapper;
import com.fyk.service.ManageService;
import com.fyk.utils.PageUtils;
import com.fyk.utils.ResultUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author qazws
 * @Date 2022/8/19 9:18
 * @Version 1.0
 */

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public ResultUtils findManage() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        Integer userId = user.getUserId();
        return new ResultUtils(200,null,userMapper.selectUserByUserId(userId));
    }

    @Override
    public ResultUtils updateManage(User user) {
        userMapper.updateUser(user);
        return new ResultUtils(200,"修改成功！",null);
    }

    @Override
    public ResultUtils userList(User user) {
        int deleted = 0;
        PageHelper.startPage(user.getPage(),user.getNum());
        List<User> users = userMapper.selectAll(deleted);
        PageInfo pageInfo = new PageInfo(users);
        List<User> list = pageInfo.getList();
        long total = pageInfo.getTotal();
        int totalPage = pageInfo.getPages();
        return findAll(list,total,totalPage);
    }

    @Override
    public ResultUtils findAll(List<User> user ,long total,int totalPage) {
        Map<String,Object> map = new HashMap<>();
        List<UserRole> userRoles = new ArrayList<>();
        for (int i = 0;i < user.size();i++){
            UserRole userRole = new UserRole();
            User user1 = user.get(i);
            List<Integer> integers = userRoleMapper.selectRoleByUsername(user1.getUsername());
            String role = "";
            for (int j = 0;j < integers.size();j++){
                if (integers.get(j) == 1){
                    role += " " + "admin" + " ";
                }else {
                    role += " " + "simple" + " ";
                }
            }
            userRole.setUser(user1);
            userRole.setRole(role);
            userRoles.add(userRole);
        }
        map.put("total",total);
        map.put("totalPage",totalPage);
        map.put("list",userRoles);
        return new ResultUtils(200,null,map);
    }

    @Override
    public ResultUtils find0User(User user) {
        user.setDeleted(0);
        PageHelper.startPage(user.getPage(),user.getNum());
        List<User> users = userMapper.selectUserByUserIdOrUsername(user);
        PageInfo pageInfo = new PageInfo(users);
        List<User> list = pageInfo.getList();
        long total = pageInfo.getTotal();
        int totalPage = pageInfo.getPages();
        return findAll(list,total,totalPage);
    }

    @Override
    public ResultUtils updateUser(User user) {
        user.setDeleted(0);
        userMapper.updateUser(user);
        List<Integer> integers = userRoleMapper.selectRoleByUsername(user.getUsername());
        for (int i = 0;i < integers.size();i++){
            if (integers.get(i) == 1){
                if (!"admin".equals(user.getRole())){
                    userRoleMapper.deleteRoleByUserId(user.getUserId(),1);
                }
            }
        }
        if ("admin".equals(user.getRole())){
            userRoleMapper.insertUserAndRole(user.getUserId(),1);
        }
        return new ResultUtils(200,"修改成功",null);
    }

    @Override
    public ResultUtils addUser(User user) {
        user.setDeleted(0);
        user.setJoinData(new Date());
        if (userMapper.selectUserByUsername(user.getUsername()) != null){
            return new ResultUtils(500,"用户名不能相同！",null);
        }
        if (user.getPassword() == "" || user.getUsername() == ""){
            return new ResultUtils(500,"用户名和密码不能为空！",null);
        }
        userMapper.insertUser(user);
        Integer userId = userMapper.selectUserByUsername(user.getUsername()).getUserId();
        if ("admin".equals(user.getRole())){
            userRoleMapper.insertUserAndRole(userId,1);
        }
        userRoleMapper.insertUserAndRole(userId,2);

        return new ResultUtils(200,"添加成功！",null);
    }

    @Override
    public ResultUtils deletedUser(User user) {
        user.setDeleted(1);
        userMapper.updateUser(user);
        return new ResultUtils(200,"删除成功",null);
    }

    @Override
    public ResultUtils deleteUserList(User user) {
        int deleted = 1;
        PageHelper.startPage(user.getPage(),user.getNum());
        List<User> users = userMapper.selectAll(deleted);
        PageInfo pageInfo = new PageInfo(users);
        List<User> list = pageInfo.getList();
        long total = pageInfo.getTotal();
        int totalPage = pageInfo.getPages();
        return findAll(list,total,totalPage);
    }

    @Override
    public ResultUtils find1User(User user) {
        user.setDeleted(1);
        PageHelper.startPage(user.getPage(),user.getNum());
        List<User> users = userMapper.selectUserByUserIdOrUsername(user);
        PageInfo pageInfo = new PageInfo(users);
        List<User> list = pageInfo.getList();
        long total = pageInfo.getTotal();
        int totalPage = pageInfo.getPages();
        return findAll(list,total,totalPage);
    }

    @Override
    public ResultUtils deleteUser(User user) {
        userMapper.deleteUser(user.getUserId());
        userRoleMapper.deleteRoleByUserId(user.getUserId(),0);
        userRoleMapper.deleteUserAndRole(user.getUserId());
        return new ResultUtils(200,"删除成功",null);
    }

    @Override
    public ResultUtils returnUser(User user) {
        user.setDeleted(0);
        userMapper.updateUser(user);
        return new ResultUtils(200,"恢复成功",null);
    }

    @Override
    public ResultUtils delAllUser(User user) {
        String[] strings = user.getBitId().split(",");
        List<String> list = Arrays.asList(strings);
        if(userMapper.deletedAllUser(list) == 0){
            return new ResultUtils(500,"删除失败！",null);
        }
        return new ResultUtils(200,"删除成功！",null);
    }

    @Override
    public ResultUtils returnAllUser(User user) {
        String[] strings = user.getBitId().split(",");
        List<String> list = Arrays.asList(strings);
        if(userMapper.returnAllUser(list) == 0){
            return new ResultUtils(500,"恢复失败！",null);
        }
        return new ResultUtils(200,"恢复成功！",null);
    }


}
